<template>
	<om-page :title="pageTitle">
		<ot-sl-filter :ref="'slFilter'" :topFixed="true" :menuList="filterList" @result="onChangeFilter" />
		<view class="page__bd">
			<uni-list>
				<view v-for="(item, index) in dataList" :key="index" class="item">
					<uni-list-item 
						:title="'资源项目类别：'+item.itemCateName"
						:avatar="index + 1"
						:avatarBgColor="calcBgColor(item)"
						@click="infoTab(item)"
					>
					<view slot="note">
						<view>项目名称：{{item.itemName}}</view>
					</view>
					</uni-list-item>
				</view>
			</uni-list>
		</view>
		<tui-scroll-top :scrollTop="scrollTop" style="background: rgba(124, 174, 255, 0.5)"></tui-scroll-top>
	</om-page>
</template>

<script>
	import { ignoreHtml } from '@/static/js/common.js';
	export default {
		data() {
			return {
				selectYear: '',
				selectMonth: '',
				selectResources: '',
				pageTotal: 1,
				pageNum: 1,
				pageSize: 20,
				scrollTop: 0,
				pageTitle:"年度资源需求",
				dataList: [],
				filterList: [{
					title: '年度',
					defaultTitle: '年度',
					detailTitle: '请选择年度',
					key: 'year',
					isSort: true,
					reflexTitle: true,
					defaultSelectedIndex: 0,
					detailList: [{
						'title': '年度',
						'value': ''
					}]
				},{
					title: '月度',
					defaultTitle: '月度',
					detailTitle: '请选择月度',
					key: 'monthCode',
					isSort: true,
					reflexTitle: true,
					defaultSelectedIndex: 0,
					detailList: [{
						'title': '月度',
						'value': ''
					}]
				},{
					title: '资源类别',
					defaultTitle: '资源类别',
					placeholder: '请选择资源类别',
					key: 'itemCate',
					isSort: true,
					reflexTitle: true,
					defaultSelectedIndex: 0,
					detailList: [{
						'title': '资源类别',
						'value': ''
					}]
				},{
					title: '部门',
					defaultTitle: '部门',
					detailTitle: '请选择部门',
					key: 'instIds',
					isTree: true,
					reflexTitle: true,
					defaultSelectedText: '',
					defaultSelectedValue: '',
					detailList: [{
						'title': '不限',
						'value': ''
					}]
				}],
			};
		},
		provide() {
			return {
				searchKey: this
			}
		},
		methods: {
			infoTab(item){
				let _that = this;
				let title = item.busName
				
				_that.om.request("ttableColumn/getGridModel",{
					tableMark: 'KEY_197'
				}).then(res=>{
					let col = res.value.columns[0]
					let arr = [];
					for(let i=0; i<col.length; i++){
						let obj = {};
						obj.name = col[i].title
						if(typeof(item[col[i].field]) == "string"){
							obj.value = ignoreHtml(item[col[i].field])
						}else{
							obj.value = item[col[i].field]
						}
						
						arr.push(obj)
					}
					arr.push({
						name: '编码',
						value: item.busCode
					})
					_that.om.getPageInfo(arr, title, {});
				})
			},
			calcBgColor(item) {
				return this.ColorList[3].name
			},
			changeArr(otit, ntit, oval, nval, data){
				for(let i=0; i<data.length; i++){
					if(otit){
						data[i][ntit] = data[i][otit]
					}
					if(oval){
						data[i][nval] = data[i][oval]
					}
				}
				return data;
			},
			onChangeFilter(data){
				let _that = this;
				_that.dataList = []
				_that.pageNum = 1
				data.page = 1
				data.rows = _that.pageSize
				_that.selectYear = data.year
				_that.selectMonth = data.monthCode
				_that.selectResources = data.itemCate
				_that.getDataList(data)
			},
			getDataList(data){
				let _that = this;
				let obj = data || {};
				_that.om.request("resourceList/getDataGrid",obj).then(res=>{
					_that.dataList = _that.dataList.concat(res.rows)
					_that.pageTotal = res.totalPage
				})
			}
		},
		onLoad(data) {
			let _that = this;
			
			uni.setNavigationBarTitle({
				title: '年度资源需求'
			});
			
			let arr = [_that.om.request("resourceList/getYearList",{},'post'),
			_that.om.request("dictZero/getListByCode",{
				code: 35803,
			},'post'), _that.om.request("dictZero/getListByCode",{
				code: 33200,
			},'post'),
			 _that.om.request("tinstitution/getComboTree")]
			//多个AJAX全部完成时开始执行
			Promise.all(arr).then(res => {
				let ylist = res[0].value;
				let yarr = []
				for(let i=0; i<ylist.length; i++){
					let obj = {}
					obj.title = ylist[i]
					obj.value = ylist[i]
					yarr.push(obj)
				}
				
				let yparr = [{
					'title': '年度',
					'value': ''
				}]
				let mparr = [{
					'title': '月度',
					'value': ''
				}]
				let zparr = [{
					'title': '资源类别',
					'value': ''
				}]
				_that.filterList[0].detailList = yparr.concat(_that._.cloneDeep(yarr))
				_that.filterList[1].detailList = mparr.concat(_that._.cloneDeep(_that.changeArr('dicname', 'title', 'code', 'value', res[1])))
				_that.filterList[2].detailList = zparr.concat(_that._.cloneDeep(_that.changeArr('dicname', 'title', 'code', 'value', res[2])))
				_that.filterList[3].detailList = _that._.cloneDeep(res[3])
				
				_that.$refs.slFilter.resetMenuList(_that.filterList)
				
				if(data.workId){
					let obj={
						page: _that.pageNum,
						rows: _that.pageSize,
						workId: data.workId,
						instIds: data.instIds,
					}
					if(data.instIds){
						_that.filterList[1].defaultSelectedValue = data.instIds
						_that.filterList[1].defaultSelectedText = data.instName
						_that.filterList[1].title = data.instName
					}
					_that.getDataList(obj)
				}else{
					let obj={
						page: _that.pageNum,
						rows: _that.pageSize,
					}
					_that.getDataList(obj)
				}
				
			})
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		// 页面上拉触底事件的处理函数
		onReachBottom: function() {
			let _that = this;
			if(_that.pageTotal>_that.pageNum){
				_that.pageNum++
				let obj = {
					page: _that.pageNum,
					rows: _that.pageSize,
					year: _that.selectYear,
					monthCode: _that.selectMonth,
					itemCate: _that.selectResources,
					instIds: _that.filterList[3].defaultSelectedValue,
				}
				_that.getDataList(obj)
			}
		}
	};
</script>

<style>
	.descFilter{
		line-height: 30px;
		padding: 0 15px;
		font-size: 14px;
		border-bottom: #EEEEEE 1px solid;
	}
</style>                                                  
                                              
